package com.MedDroid.task;

import android.os.AsyncTask;
import com.MedDroid.activity.AddMedicalRecordActivity;
import com.MedDroid.control.DatabaseControl;
import com.MedDroid.entities.User;

import java.lang.ref.WeakReference;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 03/12/13
 * Time: 2:00 AM
 * Description:
 */
public class AddNewMedicalRecordTask extends AsyncTask<Integer, Void, Void>
{
	public DatabaseControl DatabaseC;
	public WeakReference<AddMedicalRecordActivity> RecordPage;

	public AddNewMedicalRecordTask(DatabaseControl Data, AddMedicalRecordActivity activity)
	{
		DatabaseC = Data;
		RecordPage = new WeakReference<AddMedicalRecordActivity>(activity);
	}

	@Override
	protected Void doInBackground(Integer... UserID)
	{
		AddMedicalRecordActivity Page = RecordPage.get();
		User UpdatedUser = DatabaseC.findUser(User.UserIDStr,Integer.toString(UserID[0]));
		if (UpdatedUser != null)
		{
			UpdatedUser.addComment(Page.EditComment.getText().toString());
			DatabaseC.updateUser(UserID[0],UpdatedUser);
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void V)
	{
		AddMedicalRecordActivity Page = RecordPage.get();
		Page.finish();
	}
}
